#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    long long maxSum(vector<int>& nums, int m, int k) {
        unordered_map<int, int> hash;
        long long ret = 0;
        long long cur = 0;
        for (int i = 0; i < k; i++)
        {
            hash[nums[i]]++;
            cur += nums[i];
        }

        if (hash.size() >= m) ret = cur;

        int n = nums.size();
        for (int i = k; i < n; i++)
        {
            hash[nums[i - k]]--;
            cur -= nums[i - k];
            if (hash[nums[i - k]] == 0) hash.erase(nums[i - k]);

            hash[nums[i]]++;
            cur += nums[i];

            if (hash.size() >= m) ret = max(ret, cur);
        }

        return ret;
    }
};